﻿using UnityEngine;
using System.Collections;

public class FireAI : MonoBehaviour {

	public float FireIntervalInMs = 1.0f;

	public GameObject bulletPrefab;

	private float fireIntervalTimerInMs = 0.0f;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if (fireIntervalTimerInMs > -0.0001f) {
			fireIntervalTimerInMs -= Time.deltaTime;
		}
		if (fireIntervalTimerInMs < 0.0f) {
			Fire ();
		}
	}

	private void Fire () {
		if (bulletPrefab) {
			fireIntervalTimerInMs = FireIntervalInMs;
			GameObject bullet = Instantiate<GameObject>(bulletPrefab);
			Bullet bulletComponent = bullet.GetComponent<Bullet>();
			if (bulletComponent) {
				bulletComponent.SetTarget("Player");
				bulletComponent.Lunch(gameObject.transform.position, Vector3.back);
			}
		}
	}
}
